package com.pengcheng.modules.sys.controller;

import com.pengcheng.common.utils.Constant;
import com.pengcheng.common.utils.PageUtils;
import com.pengcheng.common.utils.R;
import com.pengcheng.common.valid.AddGroup;
import com.pengcheng.common.valid.UpdateGroup;
import com.pengcheng.modules.sys.entity.SysUserEntity;
import com.pengcheng.modules.sys.service.SysUserService;
import com.pengcheng.modules.sys.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 所有用户列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        //只有超级管理员，才能查看所有管理员列表
//        if(getUserId() != Constant.SUPER_ADMIN){
//            params.put("createUserId", getUserId());
//        }

        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("/save")
    public R save(@Validated({AddGroup.class}) @RequestBody SysUserEntity user) {

        user.setCreateUserId(1L);
        user.setCreateTime(new Date());
        sysUserService.save(user);

        return R.ok();
    }

    public R update(@Validated(UpdateGroup.class) @RequestBody SysUserEntity user) {

        user.setCreateUserId(1L);
        sysUserService.update(user);
        return R.ok();
    }

    @GetMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId) {
        SysUserEntity user = sysUserService.getById(userId);

        // 获取用户所属的角色列表
        List<Long> roleIdList = sysUserService.queryRoleIdList(userId);


        return R.ok().put("user",user);
    }

    @PostMapping("/delete")
    public R delete(@RequestBody Long[] userIds) {

        sysUserService.deleteBatch(userIds);

        return R.ok();
    }

    @GetMapping("/userRoleList")
    public R userRoleList(){
        List<UserRoleVo> list = sysUserService.userRoleList();
        return R.ok().put("list",list);
    }

}
